package com.iflytek.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.entity.Administrator;
import com.iflytek.entity.ResultData;
import com.iflytek.query.AdministratorQuery;
import com.iflytek.service.AdministratorService;
import com.iflytek.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("back/admin")
public class AdministratorController {

    private static final String DEFUALT_PADDWORD = "123456";

    @Autowired
    private AdministratorService administratorService;

    @GetMapping("/list")
    public String list(){
        return "/admin/list";
    }


    @ResponseBody
    @GetMapping("listData")
    public ResultData listData(AdministratorQuery query, Integer curPage, Integer pageSize){
        //设置分页参数
        PageHelper.startPage(curPage,pageSize);
        //查询数据列表
        List<Administrator> administrators = administratorService.getAdministratorList(query);
        //构建分页数据对象
        PageInfo<Administrator> pageInfo= new PageInfo<>(administrators);
        //返回分页数据
        return Result.success(pageInfo.getTotal(),pageInfo.getList());

    }

    @GetMapping("add")
    public String addUI(){
        return "admin/add";
    }

    @ResponseBody
    @PostMapping("add")
    public Map<String,Object> add(Administrator administrator){
        if (administrator == null){
            return Result.failure("添加失败");
        }
        Administrator admin = administratorService.getAdministratorByAccount(administrator.getAccount());
        if (admin != null){
            return Result.failure("添加失败，账号已经存在！");
        }
        int num = administratorService.insertAll(administrator);
        if(num == 1){
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    @GetMapping("edit")
    public String editUI(Integer adminId, Model model){
        Administrator administrator = administratorService.getAdministratorById(adminId);
        model.addAttribute("admin",administrator);
        return "admin/edit";
    }

    @ResponseBody
    @PostMapping("edit")
    public Map<String,Object> edit(Administrator administrator){
        if (administrator == null || administrator.getId()== null){
            return Result.failure("编辑失败");
        }
        int num = administratorService.update(administrator);
        if(num == 1){
            return Result.success("编辑成功");
        }
        return Result.failure("编辑失败");
    }

    @ResponseBody
    @PostMapping("delete")
    public Map<String,Object> delete(Integer adminId){
        if (adminId == null){
            return Result.failure("删除失败");
        }
        int num = administratorService.deleteById(adminId);
        if(num == 1){
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }


    @ResponseBody
    @PostMapping("batchDelete")
    public Map<String,Object> batchDelete(String adminIds){
        if (StringUtils.isEmpty(adminIds)){
            return Result.failure("删除失败");
        }
        int num = administratorService.deleteByIds(adminIds.split(","));
        if(num == 1){
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }

    @ResponseBody
    @PostMapping("resetPwd")
    public Map<String,Object> resetPwd(Integer adminId){
        if (adminId == null){
            return Result.failure("重置密码失败");
        }
        //把默认密码123456用MD5算法加密
        String md5Pwd = DigestUtils.md5DigestAsHex(DEFUALT_PADDWORD.getBytes());
        int num = administratorService.updatePassword(adminId,md5Pwd);
        if(num == 1){
            return Result.success("重置密码成功");
        }
        return Result.failure("重置密码失败");
    }


    @ResponseBody
    @PostMapping("freezeAdmin")
    public Map<String,Object> freezeAdmin(Integer adminId){
        if (adminId == null){
            return Result.failure("冻结账号失败");
        }
        int num = administratorService.updateFreeze(adminId,true);
        if(num == 1){
            return Result.success("冻结账号成功");
        }
        return Result.failure("冻结账号失败");
    }


    @ResponseBody
    @PostMapping("unfreezeAdmin")
    public Map<String,Object> unfreezeAdmin(Integer adminId){
        if (adminId == null){
            return Result.failure("解冻账号失败");
        }
        int num = administratorService.updateFreeze(adminId,false);
        if(num == 1){
            return Result.success("解冻账号成功");
        }
        return Result.failure("解冻账号失败");
    }

}


